home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / apptbk / apptbook.txt < prev    next >
Text File  |  1993-01-06  |  20KB  |  696 lines

  1. ' Variables used to manage grid
  2.  
  3. Dim IgnoreRowChange As Integer
  4. Dim GridInvertRect As RECT
  5. Dim GridInverted As Integer
  6. Dim GridDropRow As Integer
  7.  
  8. ' Drag mode constants to keep track of dragging activity.
  9.  
  10. Dim DragType As Integer         ' type of object being dragged
  11. Dim Dragging As Integer         ' TRUE when dragging is in progress
  12. Dim DragIndex As Integer        ' Optional index of dragged obj
  13. Dim DragRow As Integer          ' Optional row being dragged in grid
  14.  
  15. ' Miscellaneous variables
  16.  
  17. Dim valid%                      ' used as return for DragValid
  18.  
  19. ' Bitmasks to describe valid drag objects
  20.  
  21. Const MASK_NEWAPPT = 1      ' a new appointment
  22. Const MASK_OLDAPPT = 2      ' an old appointment
  23. Const MASK_NONE = 0         ' mask used where no drops are allowed
  24.  
  25. Function ApiRectFromPoint (ctl As Grid, X As Single, Y As Single, r As RECT) As Integer
  26.     
  27.     ' Given a grid control and a coordinate position, this routine
  28.     ' returns a Windows RECT structure containing the pixel
  29.     ' coordinates of the row being pointed at.  The row number is
  30.     ' returned, or -1, indicating that no row is being pointed at.
  31.  
  32.     Dim curRow As Integer
  33.     Dim totHeight As Single
  34.     Dim topLocation As Single
  35.  
  36.     ' Loop through each row, accumulating row height until we reach
  37.     ' the row containing the point.
  38.  
  39.     For curRow = 0 To ctl.Rows - 1
  40.  
  41.         topLocation = totHeight
  42.         totHeight = totHeight + ctl.RowHeight(curRow) + Screen.TwipsPerPixelY
  43.         
  44.         If Y < totHeight Then
  45.  
  46.             ' Convert the twips values into pixel coordinates
  47.  
  48.             ApiRectFromPoint = curRow
  49.  
  50.             r.top = topLocation / Screen.TwipsPerPixelY
  51.             r.bottom = totHeight / Screen.TwipsPerPixelY
  52.             r.left = 0
  53.             r.right = ctl.Width / Screen.TwipsPerPixelY
  54.  
  55.             Exit Function
  56.  
  57.         End If
  58.  
  59.     Next curRow
  60.  
  61.     ApiRectFromPoint = -1           ' indicate failure
  62.  
  63. End Function
  64.  
  65. Sub ApptEdit ()
  66.     
  67.     ' This subroutine moves the data in the current grid row into
  68.     ' the "post-it" editing area.
  69.  
  70.     Dim aText As String
  71.     Dim colonPos As Integer
  72.  
  73.     ' This routine copies appointment data to the edit window
  74.  
  75.     ApptList.Col = 1
  76.  
  77.     aText = ApptList.Text
  78.     colonPos = InStr(aText, ":")
  79.  
  80.     ' If no colon, there's no appointment, so clear the post-it
  81.     ' area.  If there is a colon, fill in the information.
  82.  
  83.     If colonPos = 0 Then
  84.         ApptText.Text = ""
  85.         ApptTime.Text = Format$(0, ApptTime.Format)
  86.         ApptType.Text = ""
  87.     Else
  88.         ApptType.Text = Left$(aText, colonPos - 1)
  89.         ApptText.Text = Mid$(aText, colonPos + 2)
  90.         ApptList.Col = 0
  91.         ApptTime.Text = Format$(ApptList.Text, ApptTime.Format)
  92.     End If
  93.  
  94. End Sub
  95.  
  96. Sub ApptList_DragDrop (Source As Control, X As Single, Y As Single)
  97.     
  98.     ' Drop a new appointment or existing appointment at a new
  99.     ' row position.
  100.  
  101.     Dim aText As String
  102.     Dim i%
  103.  
  104.     If Not EndDragMode(MASK_NEWAPPT Or MASK_OLDAPPT) Then Exit Sub
  105.  
  106.     UnhighlightRow
  107.     IgnoreRowChange = True
  108.  
  109.     If DragType = MASK_NEWAPPT Then
  110.         ApptList.Col = 1
  111.         ApptList.Row = GridDropRow
  112.         ApptList.Text = Source.Tag & ": "
  113.         ApptEdit
  114.     Else
  115.         ApptList.Col = 0
  116.         ApptList.Row = GridDropRow
  117.         aText = ApptList.Text
  118.         ApptList.Row = DragRow
  119.         i% = ChangeApptTime(TimeValue(aText))
  120.     End If
  121.  
  122.     IgnoreRowChange = False
  123.     ApptText.SetFocus
  124.  
  125. End Sub
  126.  
  127. Sub ApptList_DragOver (Source As Control, X As Single, Y As Single, State As Integer)
  128.     
  129.     ' When dragging over the grid, both new and old appointments
  130.     ' are considered.  For both cases, we unhighlight the current
  131.     ' destination row upon leaving the drop zone, and assure that
  132.     ' the row under the point is highlighted otherwise.
  133.  
  134.     If Not DragValid(Source, MASK_NEWAPPT Or MASK_OLDAPPT, State) Then
  135.         Exit Sub
  136.     End If
  137.  
  138.     Select Case State
  139.         Case LEAVE
  140.             UnhighlightRow
  141.         Case Else
  142.             GridDropRow = HighlightRowAtPoint(X, Y)
  143.     End Select
  144.  
  145. End Sub
  146.  
  147. Sub ApptList_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
  148.     
  149.     ' We take charge of the mouse down event to initiate dragging
  150.     ' ourselves.  First, the cursor must be in column 1.  Next,
  151.     ' the row must contain a valid appointment to be grabbed
  152.     ' (identified by the presence of a colon in the cell).
  153.  
  154.     If AtGridCol(ApptList, X, Y) > 0 Then
  155.         If InStr(ApptList.Text, ":") <> 0 Then
  156.  
  157.             ' The timer will now count down.  This allows the user
  158.             ' to easily click, or "press" the mouse.  The Timer
  159.             ' event handles the drag initialization.
  160.  
  161.             GridTimer.Enabled = True
  162.  
  163.         End If
  164.     End If
  165.  
  166. End Sub
  167.  
  168. Sub ApptList_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)
  169.  
  170.     ' Be sure the timer is disabled so that a click doesn't
  171.     ' initiate a drag.  If it's already disabled, it doesn't matter.
  172.  
  173.     GridTimer.Enabled = False
  174.  
  175. End Sub
  176.  
  177. Sub ApptList_RowColChange ()
  178.     
  179.     ' Whenever the row changes, move the highlight to track the
  180.     ' current cell.
  181.  
  182.     ApptList.SelStartRow = ApptList.Row
  183.     ApptList.SelEndRow = ApptList.Row
  184.  
  185.     ' IgnoreRowChange means that we're setting Col or Row somewhere
  186.     ' else in the code and we don't want ApptEdit to be called.
  187.     ' Otherwise, the user changed the row and we update the
  188.     ' "post-it" area.
  189.  
  190.     If Not IgnoreRowChange Then
  191.         IgnoreRowChange = True
  192.         ApptEdit
  193.         IgnoreRowChange = False
  194.     End If
  195.  
  196. End Sub
  197.  
  198. Sub ApptText_DragDrop (Source As Control, X As Single, Y As Single)
  199.     valid% = EndDragMode(MASK_NONE)
  200. End Sub
  201.  
  202. Sub ApptText_DragOver (Source As Control, X As Single, Y As Single, State As Integer)
  203.     valid% = DragValid(Source, MASK_NONE, State)
  204. End Sub
  205.  
  206. Sub ApptTime_DragDrop (Source As Control, X As Single, Y As Single)
  207.     valid% = EndDragMode(MASK_NONE)
  208. End Sub
  209.  
  210. Sub ApptTime_DragOver (Source As Control, X As Single, Y As Single, State As Integer)
  211.     valid% = DragValid(Source, MASK_NONE, State)
  212. End Sub
  213.  
  214. Sub ApptTime_ValidationError (InvalidText As String, StartPosition As Integer)
  215.     
  216.     MsgBox "Invalid time"
  217.     ApptTime.SetFocus
  218.  
  219. End Sub
  220.  
  221. Sub ApptType_DragDrop (Source As Control, X As Single, Y As Single)
  222.     
  223.     ' Accept a drop only for a NEWAPPT icon, otherwise the
  224.     ' operation will be cancelled.
  225.  
  226.     If EndDragMode(MASK_NEWAPPT) Then
  227.         ApptType.Text = Source.Tag
  228.     End If
  229.  
  230. End Sub
  231.  
  232. Sub ApptType_DragOver (Source As Control, X As Single, Y As Single, State As Integer)
  233.     valid% = DragValid(Source, MASK_NEWAPPT, State)
  234. End Sub
  235.  
  236. Sub ApptType_KeyPress (KeyAscii As Integer)
  237.     
  238.     ' Don't allow a colon to be entered, since we use a colon to
  239.     ' separate the appointment "kind" from the text.
  240.  
  241.     If KeyAscii = Asc(":") Then
  242.         Beep
  243.         KeyAscii = 0
  244.     End If
  245.  
  246. End Sub
  247.  
  248. Function AtGridCol (ctl As Control, X As Single, Y As Single)
  249.     
  250.     ' Given a point on a grid control, in twips, this routine
  251.     ' returns the column number where the point is located, or
  252.     ' -1 indicating the point is outside the grid.
  253.  
  254.     Dim curCol As Integer
  255.     Dim totWidth As Single
  256.  
  257.     ' Loop through each column, accumulating column width until we
  258.     ' reach the column containing the point.
  259.  
  260.     For curCol = 0 To ctl.Cols - 1
  261.         
  262.         totWidth = totWidth + ctl.ColWidth(curCol) + Screen.TwipsPerPixelX
  263.         
  264.         If X < totWidth Then
  265.             AtGridCol = curCol
  266.             Exit Function
  267.         End If
  268.  
  269.     Next curCol
  270.  
  271.     AtGridCol = -1          ' not found
  272.  
  273. End Function
  274.  
  275. Sub BeginDragMode (ctl As Control, objType As Integer)
  276.     
  277.     ' Whenever a drag is about to start, this routine is called.
  278.     ' The type mask of the drag is flagged, and we remember that
  279.     ' dragging is in progress.   This routine MUST be matched
  280.     ' by an EndDragMode function call.
  281.  
  282.     DragType = objType
  283.     Dragging = True
  284.  
  285.     ' S